/**
 *
 * @Description 邮件发送
 * 调用方法:sendMail('123456789@qq.com','这是测试邮件', 'Hello World');
 * @Author blcyzycc
 * @Created 2018/05/09 15:10
 *
 * 163邮箱
 * 注意：内容中不能有html标签,会导致：554 DT:SPM 发送的邮件内容包含了未被许可的信息，或被系统识别为垃圾邮件。
 */


const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
    host: "smtp.163.com", // 主机
    port: 465, // SMTP 端口
    secure: true, //两个都要设置
    secureConnection: true, //两个都要设置
    auth: {
        user: '18202762822@163.com',
        pass: 'ye31fsfgsd97dsfe'
    }
});

/**
 * @param {String} recipient 收件人
 * @param {String} subject 发送的主题
 * @param {String} html 发送的html内容
 */

let sendMail = function (recipient, subject, html, callback) {
    transporter.sendMail(
        {
            from: "18202762822@163.com", // 发件地址
            to: recipient, // 收件邮箱
            subject: subject, // 标题
            html: html //html内容
        },
        (error, info) => {
            callback(error, info);
            transporter.close()// 关闭连接池
        }
    );
}

/*
let sendMail = function (recipient, subject, html) {
    let option = {
        from: "18202762822@163.com", // 发件地址
        to: recipient, // 收件邮箱
        subject: subject, // 标题
        html: html //html内容
    }
    transporter.sendMail(option, (error, info) => {
            if (error) {
                return console.log('mail',error);
            }
            console.log('Message sent:', info.response);
            transporter.close()// 关闭连接池
        }
    );
}*/

module.exports = sendMail
